package com.backom.example.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.stereotype.Repository;

import com.backom.example.domain.User;

@Repository
public class UserDao {

	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int getMatchCount(String userName, String password) {
		String sqlStr = "SELECT count(*) FROM t_user "
				+ "WHERE user_name = ? and password = ?";
		return jdbcTemplate.queryForInt(sqlStr, new Object[]{userName,password});
	}
	
	public User findUserByUserName(String userName) {
		String sqlStr = "SELECT user_id,user_name,credits" +
				"FROM t_user WHERE user_name = ?";
		User user = new User();
		
		jdbcTemplate.query(sqlStr, new Object[]{userName}, new RowCallbackHandler() {
			
			@Override
			public void processRow(ResultSet rs) throws SQLException {
				// TODO Auto-generated method stub
				user.setUserId(rs.getInt("user_id"));
				user.setUserName(userName);
				user.setCredits(rs.getInt("credits"));
			}
		});
		
		return user;
	}
	
	public void updateUserInfo(User user) {
		String sqlStr = "UPDATE t_user SET last_visit = ?,last_ip = ?, credits = ?" 
				+ "WHERE user_id = ?";
		jdbcTemplate.update(sqlStr, new Object[]{user.getLastVisit(),user.getLastIp(),user.getCredits(),user.getUserId()});
	}
}

